-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use PyModule_New in module_ public constructor #2551
base: master
Are you sure you want to change the base?
Conversation
FWIW, I think we should probably drop |
Right. So in that case, we cannot add this PR without breaking existing usage of |
To be totally honest, without having a full appreciation of all the details in this PR, I feel like this is getting way too complicated for very little gain. |
@rwgk Thanks for the review! I'm not sure I fully understand what you mean, but let me try to answer, anyway: This PR's diff looks horrible, but actually isn't that huge or complex, I believe. The main thing this PR tries to make sure we can treat But yes, I think this PR is too much to still include in 2.6.0. I've created #2552, which basically just deprecates the public constructor. As far as I'm concerned, this is basically just a formalization of your "please don't use this directly, only use our macros" comment, no? (One that's still ignorable, but less likely to be accidentally overlooked; after all, who reads the docs, let alone the comments?) Would you mind giving #2552 a look? I've summarized the actual changes (vs. the perceived amount of diff): #2552 (comment)
As far as I know, Python 2 doesn't make things a lot more complex, in this case. But yes, completely agree that making changes becáuse of Python 2 is not a good idea anymore! |
b043c6d
to
65f8c98
Compare
65f8c98
to
b479f2a
Compare
include/pybind11/pybind11.h
Outdated
explicit module_(const char *name, const char *doc = nullptr) { | ||
#if PY_MAJOR_VERSION >= 3 | ||
*this = create_extension_module(name, doc, new PyModuleDef()); | ||
#if defined(PYPY_VERSION) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sumitted https://foss.heptapod.net/pypy/pypy/-/merge_requests/769 to fix this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which got merged. So once we bump PyPy, this can be fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we wait with this PR until we bump PyPy?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should. This is really just a wrong signature (see PyPy PR) and PyPy isn't doing anything non-const, potentially dangerous with the const_cast
ed string. We also don't know when PyPy will release 7.4.0, and whether we'll immediately drop support for 7.3.x, then. Meanwhile, the core of this PR is good.
I did add a more specific (PYPY_VERSION_NUM < 0x07030400)
check, though. This should help when we do bump the minimum PyPy version supported.
b479f2a
to
2436f44
Compare
c8025b1
to
1d0660f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Google-global testing came back green for this PR, too.
Approving, but please see my other comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two minor comments, but approved.
include/pybind11/pybind11.h
Outdated
explicit module_(const char *name, const char *doc = nullptr) { | ||
#if PY_MAJOR_VERSION >= 3 | ||
*this = create_extension_module(name, doc, new PyModuleDef()); | ||
#if defined(PYPY_VERSION) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we wait with this PR until we bump PyPy?
126da01
to
10f8790
Compare
@henryiii, since you haven been involved in the discussion too much: if you have time for a quick read of the upgrade guide and consider that to be clear enough, I think we're good :-) |
10f8790
to
29675ed
Compare
Alternative to the plan in #2534 and #2413: rather than making the
py::module
constructor private, we can turn it into a "normal"py::object
constructor, and usePyModule_New
to create a newmodule
object (in the same way astypes.ModuleType("abc", "def")
would do so).Modules create with
PyModule_New
cannot be used as extension modules (unfortunately). Python (3) complains if a module object is returned that doesn't have adef
, so this is very unfortunately not a solution for thestatic PyModuleDef
:-(Things to note/judge/discuss:
PYBIND11_PLUGIN
example will nót work anymore (because thepy::module_
constructor has changed). This would also be the case if the constructor would be made private (though things would then just stop compiling). But, things don't segfault, andPYBIND11_PLUGIN
has been deprecated for ages.PYBIND11_PLUGIN
, things should work exactly the same.PYBIND11_MODULE
now usespybind11::module_::create_extension_module
, so anything that's returned as top-level extension module is correctly constructed. For all other uses, themodule_
constructor should work.detail::create_top_level_module
and just moved it to a publicstatic module_::create_extension_module
. Downside: Python 2 and 3 still differ in signature, but maybe I'm getting over that. Could be reverted, as well.py::module_
constructor. That's why I started thinking about keeping this constructor. That test still works, as the new constructor still creates a valid module object.I'm adding this to v2.6.0, as a way of making sure we come up with a plan on what to include now and what to postpone for the future. E.g., right now, we could judge to just deprecate the public
module_
constructors, have users be warned in 2.6.0, and add this change in 2.7.0?Suggested changelog entry: